package com.hd.crdao.utils;

/**
 * 该类是针对汉字进行校验
 */
public class ChineseUtils {
    /**
     * 校验每个字符是否是中文汉字
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
    }

    /**
     * 校验整个字符串是否都是汉字
     *
     * @param name
     * @return
     */
    public static boolean checkNameChese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++) {
            if (!isChinese(cTemp[i])) {
                res = false;
                break;
            }
        }
        return res;

    }

    /**
     * 返回Vin码
     * @param words
     * @return
     */
    public static String dealWithWords(String words) {
        while (words.contains(" ")){        //替换中间所有的空格
            words = words.replace(" ", "");
        }

        for (int i = 0; i < words.length(); i++) {
            char c = words.charAt(i);
            if (isChinese(c)) {
                words = words.replace(c, '_');         //把内容中所有带中文的全部替换成_
            }
        }
        while (words.contains("_")){
            words = words.replace("_", "");         //把所有下划线全部替换成空字符串
        }
        return words;
    }
}
